package com.xuzimian.opencv.demo;

import org.bytedeco.javacv.CanvasFrame;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.OpenCVFrameConverter;

import javax.swing.*;

public class FFmpemFrameDemo {

    public static void showInFrameGrabber(String rtspUri) throws FFmpegFrameGrabber.Exception {
        FFmpegFrameGrabber frameGrabber = FFmpegFrameGrabber.createDefault(rtspUri);
        frameGrabber.setOption("rtsp_transport", "tcp");
        //设置未响应超时时间 0.5秒
        frameGrabber.setOption("stimeout", "5000000");
        frameGrabber.setOption("analyzeduration", "50000");
        frameGrabber.setOption("probesize", "1020");
        //设置视频比例
        frameGrabber.setAspectRatio(1.7777);
        frameGrabber.setImageWidth(1280);
        frameGrabber.setImageHeight(720);

        frameGrabber.start();
        System.out.println("---------------------");

        CanvasFrame canvasFrame = new CanvasFrame("rtsp");

        canvasFrame.setAlwaysOnTop(true);
        canvasFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        OpenCVFrameConverter.ToMat converter = new OpenCVFrameConverter.ToMat();
        canvasFrame.setCanvasSize(1280, 720);

        while (true) {
            Frame frame = frameGrabber.grabImage();
            frame.imageHeight = 720;
            frame.imageWidth = 1280;
            converter.convertToMat(frame);
            canvasFrame.showImage(frame);
        }
    }

}
